package example;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.rmi.RemoteException;
import java.util.*;

import costEstimation.Optimizer;
import costEstimation.Optimizer2;

import optimization.Condition;
import optimization.ConditionFieldIndex;
import optimization.Field;
import optimization.SQLParsingException;
import optimization.tab_name_att_name;

import queryTree.AbstractNode;
import queryTree.JoinNode;
import queryTree.Main;
import queryTree.TableNode;
import queryTree.TreeNode;

public class util {
	public static int CLENGTH = 20; 
	private static Random rn = new Random();
	Writer output;
	String fileResult = "database.sgml"; 
	public static String lookupHost(String tableName) throws IOException, Exception {
		String host = "";
		DBConnection dbConn = new DBConnection();
		String selectHost = "Select host from distributedHost where tableName = '"+tableName.toLowerCase()+"'  limit 0,1";
		host = dbConn.getSingleRow(selectHost);
		return host;
	}
	public int rand(int lo, int hi)
     {
             int n = hi - lo + 1;
             int i = rn.nextInt() % n;
             if (i < 0)
                     i = -i;
             return lo + i;
     }

	public int genNumber(){
		int lo = 10;
		int hi = 100;
		int a = rand(lo,hi);
		return a;
	}
	@SuppressWarnings("deprecation")
	public String genString(){
		int lo = 2;
		int hi = 11;
		int n = rand(lo, hi);
        byte b[] = new byte[n];
        for (int i = 0; i < n; i++)
                b[i] = (byte)rand('a', 'z');
        return new String(b, 0);
	}
	public static String getControlNodeQuery(JoinNode t ) {
		String sql = "";
		return sql;
	}
	public static String getInterNodeQuery(JoinNode t ) {
		String sql = "";
		return sql;
	}
	public static String getRemoteNodeQuery(JoinNode t ) {
		String sql = "";
		return sql;
	}
//	public MixProtocol buildMessage2(JoinNode t, String controlSiteTable) throws SQLParsingException {
//		MixProtocol msg = new MixProtocol();
//		TreeNode leftNode1 = (TreeNode) t.getLeftJoinTree();
//		TreeNode rightNode1 = (TreeNode) t.getRightJoinTree();
//		if((leftNode1 instanceof JoinNode)) {	
//			TableNode leftLeaf1 = (TableNode) ((JoinNode) leftNode1).getLeftJoinTree();
//			TableNode rightLeaf1 = (TableNode) ((JoinNode) leftNode1).getRightJoinTree();
//			msg.joinNode1Query = ((TableNode) rightNode1).getLocalSQL();
//			msg.JoinNode1 = ((TableNode) rightNode1).getTableName();	
//			msg.JoinNode2 = rightLeaf1.getTableName();
//			msg.joinNode2Query = rightLeaf1.getLocalSQL();
//			msg.remoteSite = leftLeaf1.getTableName();
//			msg.remotesiteQuery = leftLeaf1.getLocalSQL();
//			msg.intersiteQuery = msg.joinNode2Query;
//			msg.interSite = msg.JoinNode2;
//			
//			if(controlSiteTable.toUpperCase().equals(rightLeaf1.getTableName().toUpperCase())) {
//				msg.controlsiteQuery = rightLeaf1.getLocalSQL();
//			}
//			else if(controlSiteTable.toUpperCase().equals(leftLeaf1.getTableName().toUpperCase())) {
//				msg.controlsiteQuery = leftLeaf1.getLocalSQL();
//			}
//			else if(controlSiteTable.toUpperCase().equals(((TableNode) rightNode1).getTableName().toUpperCase())) {
//				msg.controlsiteQuery = ((TableNode) rightNode1).getLocalSQL();
//			}
//			
//			ArrayList<Condition> joinNode2ConditionList = ((JoinNode) leftNode1).getJoinConditionList();
//			Condition cond2  = joinNode2ConditionList.get(0);
//			Field lfield2 = cond2.lhs;
//			Field rfield2 = cond2.rhs;
//			int type2 = (joinNode2ConditionList.size()==0) ? 0 : 1;
//			msg.joinNode2Condition = new ConditionFieldIndex(this.getIndexField(lfield2.item_name,leftLeaf1.getAttrsProjection()),getIndexField(rfield2.item_name,rightLeaf1.getAttrsProjection()),type2);
//			ArrayList<Condition> joinNode1ConditionList = t.getJoinConditionList();
//			Condition cond1  = joinNode1ConditionList.get(0);
//			Field lfield = cond1.lhs;
//			Field rfield = cond1.rhs;
//			int type1 = (joinNode2ConditionList.size()==0) ? 0 : 1;
//			msg.joinNode1Condition = new ConditionFieldIndex(this.getIndexField(lfield.item_name,rightLeaf1.getAttrsProjection()),getIndexField(rfield.item_name,((AbstractNode) rightNode1).getAttrsProjection()),type1);
//		}
//		return msg;
//	}
//	public MixProtocol buildMessage(JoinNode t, String controlSiteTable) throws SQLParsingException {
//		MixProtocol msg = new MixProtocol();
//		TreeNode leftNode1 = (TreeNode) t.getLeftJoinTree();
//		TreeNode rightNode1 = (TreeNode) t.getRightJoinTree();
//		if((leftNode1 instanceof JoinNode)) {	
//			TableNode leftLeaf1 = (TableNode) ((JoinNode) leftNode1).getLeftJoinTree();
//			TableNode rightLeaf1 = (TableNode) ((JoinNode) leftNode1).getRightJoinTree();
//			msg.JoinNode2 = leftLeaf1.getTableName();
//			msg.joinNode2Query = leftLeaf1.getLocalSQL();
//			msg.remoteSite = rightLeaf1.getTableName();
//			msg.remotesiteQuery = rightLeaf1.getLocalSQL();
//		
//			msg.JoinNode1 = ((TableNode) rightNode1).getTableName();
//			msg.joinNode1Query = ((TableNode) rightNode1).getLocalSQL();
//			
//			msg.intersiteQuery = msg.joinNode2Query;
//			msg.interSite = msg.JoinNode2;
//			if(controlSiteTable.toUpperCase().equals(rightLeaf1.getTableName().toUpperCase())) {
//				msg.controlsiteQuery = rightLeaf1.getLocalSQL();
//			}
//			else if(controlSiteTable.toUpperCase().equals(leftLeaf1.getTableName().toUpperCase())) {
//				msg.controlsiteQuery = leftLeaf1.getLocalSQL();
//			}
//			else if(controlSiteTable.toUpperCase().equals(((TableNode) rightNode1).getTableName().toUpperCase())) {
//				msg.controlsiteQuery = ((TableNode) rightNode1).getLocalSQL();
//			}
//			ArrayList<Condition> joinNode2ConditionList = ((JoinNode) leftNode1).getJoinConditionList();
//			if(joinNode2ConditionList.size()!=0) {
//				Condition cond2  = joinNode2ConditionList.get(0);
//				Field lfield2 = cond2.lhs;
//				Field rfield2 = cond2.rhs;
//				int type2 = (joinNode2ConditionList.size()==0) ? 0 : 1;
//				msg.joinNode2Condition = new ConditionFieldIndex(this.getIndexField(lfield2.item_name,leftLeaf1.getAttrsProjection()),getIndexField(rfield2.item_name,rightLeaf1.getAttrsProjection()),type2);
//			}
//			
//			ArrayList<Condition> joinNode1ConditionList = t.getJoinConditionList();
//			if(joinNode1ConditionList.size()!=0) {
//				Condition cond1  = joinNode1ConditionList.get(0);
//				Field lfield = cond1.lhs;
//				Field rfield = cond1.rhs;
//				int type1 = (joinNode1ConditionList.size()==0) ? 0 : 1;
//				msg.joinNode1Condition = new ConditionFieldIndex(this.getIndexField(lfield.item_name,rightLeaf1.getAttrsProjection()),getIndexField(rfield.item_name,((AbstractNode) rightNode1).getAttrsProjection()),type1);
//			}
//		}
//		else if((rightNode1 instanceof JoinNode)) {	
//			TableNode leftLeaf1 = (TableNode) ((JoinNode) rightNode1).getLeftJoinTree();
//			TableNode rightLeaf1 = (TableNode) ((JoinNode) rightNode1).getRightJoinTree();
//			msg.JoinNode2 = leftLeaf1.getTableName();
//			msg.joinNode2Query = leftLeaf1.getLocalSQL();
//			msg.remoteSite = rightLeaf1.getTableName();
//			msg.remotesiteQuery = rightLeaf1.getLocalSQL();
//		
//			msg.JoinNode1 = ((TableNode) rightNode1).getTableName();
//			msg.joinNode1Query = ((TableNode) rightNode1).getLocalSQL();
//			
//			msg.intersiteQuery = msg.joinNode2Query;
//			msg.interSite = msg.JoinNode2;
//			if(controlSiteTable.toUpperCase().equals(rightLeaf1.getTableName().toUpperCase())) {
//				msg.controlsiteQuery = rightLeaf1.getLocalSQL();
//			}
//			else if(controlSiteTable.toUpperCase().equals(leftLeaf1.getTableName().toUpperCase())) {
//				msg.controlsiteQuery = leftLeaf1.getLocalSQL();
//			}
//			else if(controlSiteTable.toUpperCase().equals(((TableNode) rightNode1).getTableName().toUpperCase())) {
//				msg.controlsiteQuery = ((TableNode) rightNode1).getLocalSQL();
//			}
//			ArrayList<Condition> joinNode2ConditionList = ((JoinNode) rightNode1).getJoinConditionList();
//			if(joinNode2ConditionList.size()!=0) {
//				Condition cond2  = joinNode2ConditionList.get(0);
//				Field lfield2 = cond2.lhs;
//				Field rfield2 = cond2.rhs;
//				int type2 = (joinNode2ConditionList.size()==0) ? 0 : 1;
//				msg.joinNode2Condition = new ConditionFieldIndex(this.getIndexField(lfield2.item_name,leftLeaf1.getAttrsProjection()),getIndexField(rfield2.item_name,rightLeaf1.getAttrsProjection()),type2);
//			}
//			
//			ArrayList<Condition> joinNode1ConditionList = t.getJoinConditionList();
//			if(joinNode1ConditionList.size()!=0) {
//				Condition cond1  = joinNode1ConditionList.get(0);
//				Field lfield = cond1.lhs;
//				Field rfield = cond1.rhs;
//				int type1 = (joinNode1ConditionList.size()==0) ? 0 : 1;
//				msg.joinNode1Condition = new ConditionFieldIndex(this.getIndexField(lfield.item_name,rightLeaf1.getAttrsProjection()),getIndexField(rfield.item_name,((AbstractNode) rightNode1).getAttrsProjection()),type1);
//			}
//		}
//		return msg;
//	}
	private int getIndexField(tab_name_att_name item_name,
			 ArrayList<Field> attrsProjection) {
		// TODO Auto-generated method stub
		int i = 0;
		boolean found = false;
		while(i<attrsProjection.size()) {
			if(item_name.toString().toUpperCase().equals(attrsProjection.get(i).item_name.toString().toUpperCase())) {				
				return i;
			}
			i++;
		}
		i = (found) ? i : -1 ;
		return 0;
	}
	public static void selectionSort(ArrayList<Tuples> x) {
	    for (int i=0; i<x.size()-1; i++) {
	        for (int j=i+1; j<x.size(); j++) {
	            if (x.get(i).tups.get(0).toString().compareTo(x.get(j).tups.get(0))>0) {
	                String temp = x.get(i).tups.get(0);
	                x.get(i).tups.set(0, x.get(j).tups.get(0));
	                x.get(j).tups.set(0, temp);
	            }
	        }
	    }
	}
	public static void selectionSort1(int[] x) {
	    for (int i=0; i<x.length-1; i++) {
	        for (int j=i+1; j<x.length; j++) {
	            if (x[i] > x[j]) {
	                int temp = x[i];
	                x[i] = x[j];
	                x[j] = temp;
	            }
	        }
	    }
	}
}
